package custom_view;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;
import com.wefika.flowlayout.FlowLayout;

import java.util.ArrayList;

import adapter.user_management.SuggestUserInInviteUserToCompanyAdapter;
import controller.group_management.DownloadCircleImageViewInMemberTabCircleImageViewAsync;
import controller.user_management.GetUserInfoFromContactAsync;
import de.hdodenhof.circleimageview.CircleImageView;
import define.API;
import ui.activity.user_managament.InviteUserToCompany;
import ui.fragment.group_management.CreateGroupFragment;

/**
 * Created by trek2000 on 23/8/2014.
 */
public class InviteUserTabLinearLayout extends LinearLayout {
    /**
     * String session
     */
    public static String EMAIL = "";
    /**
     * Views session
     */
    public static TextView mTvUserEmail;
    public static ImageButton mIbtnRemoveUserEmail;
    /**
     * The others secssion
     */
    public static Context mContext;

    public static InviteUserTabLinearLayout newInstance(Context context, String email) {
        mContext = context;
        EMAIL = email;
        InviteUserTabLinearLayout inviteUserTabLinearLayout =
                new InviteUserTabLinearLayout(context);
        inviteUserTabLinearLayout.addView(mTvUserEmail);
        inviteUserTabLinearLayout.addView(mIbtnRemoveUserEmail);
        return inviteUserTabLinearLayout;
    }

    public InviteUserTabLinearLayout(Context context) {
        super(context);
        // Set background color
        setBackgroundResource(R.drawable.custom_btn_comp_name);

        // Layout params for margin
        FlowLayout.LayoutParams layoutParams = new FlowLayout.LayoutParams(
                FlowLayout.LayoutParams.WRAP_CONTENT, 70);
//        layoutParams.gravity = Gravity.LEFT|Gravity.CENTER;
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.setMargins(5, 5, 5, 5);
//        layoutParams.setMargins(10, 5, 10, 5);
        setLayoutParams(layoutParams);
        // Set padding
        setPadding(10, 10, 10, 10);
//        setPadding(5, 0, 5, 0);
        //set orientation
        setOrientation(HORIZONTAL);
        //init views
        initialViews ();
        //set text for text view
        loadTextViewUserEmail(EMAIL);
        //layout gravity for image button remove
        loadImageButtonRemoveUserEmail();
        // onclick for image button remove
        onClickImageButtonRemoveUserEmail();
    }

    private void loadTextViewUserEmail (String email) {
        LinearLayout.LayoutParams layoutParamsForTextViewUserEmail = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//                FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.MATCH_PARENT);
        layoutParamsForTextViewUserEmail.gravity = Gravity.CENTER;
//        layoutParamsForTextViewUserEmail.setMargins(25, 25, 20, 25);
        layoutParamsForTextViewUserEmail.setMargins(5, 5, 5, 5);

        mTvUserEmail.setLayoutParams(layoutParamsForTextViewUserEmail);
        mTvUserEmail.setTextColor(getResources().getColor(R.color.black));
        mTvUserEmail.setText(email);
        mTvUserEmail.requestLayout();
    }
    private void loadImageButtonRemoveUserEmail() {
        FlowLayout.LayoutParams layoutParamsForImageButtonRemoveUserEmail = new FlowLayout.LayoutParams(
                FlowLayout.LayoutParams.MATCH_PARENT, FlowLayout.LayoutParams.MATCH_PARENT);
//                FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.MATCH_PARENT);
        layoutParamsForImageButtonRemoveUserEmail.gravity = Gravity.RIGHT|Gravity.CENTER;
        layoutParamsForImageButtonRemoveUserEmail.setMargins(10, 5, 5, 5);

        mIbtnRemoveUserEmail.setLayoutParams(layoutParamsForImageButtonRemoveUserEmail);
        mIbtnRemoveUserEmail.setBackgroundResource(R.drawable.ibtn_remove);
//        mIbtnRemoveUserEmail.setPadding(10, 10, 10, 10);
        mIbtnRemoveUserEmail.requestLayout();
//        mIbtnRemoveUserEmail.setPadding(0, 5, 0, 5);
    }

    private void onClickImageButtonRemoveUserEmail() {
        mIbtnRemoveUserEmail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * Flowlayout add InviteUserTabLinearLayout(Linearlayout), InviteUserTabLinearLayout add image button
                 * image button set onclick
                 * v.getParent() => InviteUserTabLinearLayout(Linearlayout), v.getParent().getParent() => Flowlayout
                 */
                switch(((FlowLayout) v.getParent().getParent()).getId()){
                    case R.id.flowlayout_in_invite_user_to_company_activity:
                        //reset check box unchecked, compare email in array invite with email in array check box checked
                        for (int  p = 0 ; p < SuggestUserInInviteUserToCompanyAdapter.
                                mAlPosOfItemCheckedInSuggestUser.size(); p++) {
                            if (GetUserInfoFromContactAsync.mAlUserInfoFromContact.get(
                                    SuggestUserInInviteUserToCompanyAdapter.
                                            mAlPosOfItemCheckedInSuggestUser.get(p)
                            ).getUserEmail().equals(
                                    InviteUserToCompany.mAlInviteUserEmail.get(
                                            Integer.valueOf(((LinearLayout)v.getParent()).getTag().toString())
                                    )
                            )){
                                SuggestUserInInviteUserToCompanyAdapter.
                                        mAlPosOfItemCheckedInSuggestUser.remove(p);
                                InviteUserToCompany.suggestUserInInviteUserToCompanyAdapter.notifyDataSetChanged();
                            }
                        }
                        //remove view
                        InviteUserToCompany.mflowlayoutInInviteUserToCompany.removeViewAt(
                                Integer.valueOf(((LinearLayout)v.getParent()).getTag().toString())
                        );
                        //remove data
                        InviteUserToCompany.mAlInviteUserEmail.remove(
                                InviteUserToCompany.mAlInviteUserEmail.get(
                                        Integer.valueOf(((LinearLayout)v.getParent()).getTag().toString())
                                )
                        );
                        //reset tag for each item of flow layout after remove one item
                        for (int t = Integer.valueOf(((LinearLayout)v.getParent()).getTag().toString());
                             t < InviteUserToCompany.mflowlayoutInInviteUserToCompany.getChildCount() - 1; t++) {
                            InviteUserToCompany.mflowlayoutInInviteUserToCompany.getChildAt(t).setTag(t);
                        }
                        //set text for mTvInviteInInviteUserToCompany
                        InviteUserToCompany.setTextForTextViewInviteInInviteUserToCompany(
                                InviteUserToCompany.mAlInviteUserEmail);
                        break;
                }

            }
        });
    }

    private void initialViews () {
        mTvUserEmail = new TextView(mContext);
        mIbtnRemoveUserEmail = new ImageButton(mContext);
    }
}
